package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsUserMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsUser;
import com.qf.service.DtsUserService;
import com.qf.vo.DayStatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class DtsUserServiceImpl implements DtsUserService {

    @Autowired
    DtsUserMapper userMapper;

    @Autowired
    StatMapper statMapper;

    @Override
    public Integer findTotal() {
        LambdaQueryWrapper<DtsUser> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(DtsUser::getDeleted, false);

        Integer count = userMapper.selectCount(wrapper);

        return count;
    }

    @Override
    public List<DayStatis> queryDayStatis(int rang) {

        return statMapper.queryDayStatis(rang);
    }

    @Override
    public IPage<DtsUser> queryPage(Integer page, Integer limit, String sort, String order, String username, String mobile) {

        IPage<DtsUser> iPage = new Page<>(page, limit);

        QueryWrapper<DtsUser> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(username)){
            wrapper.like("username", username);
        }

        if(!StringUtils.isEmpty(mobile)){
            wrapper.like("mobile", mobile);
        }

        if(order.equalsIgnoreCase("desc")){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }

        iPage = userMapper.selectPage(iPage, wrapper);

        return iPage;
    }
}
